<template>

  <div class="user-info">
    <div class="add"  @click="download()">下载</div>
    <eagle-sing-diy v-if="zIndex==8" ref="setzIndex" :isContinue="true" :mzIndex="zIndex" :id="id" v-on:FilePathClick="FilePathClick" ></eagle-sing-diy>


    <div class="read-pdf">
      <pdf
        ref="pdf"
        v-for="i in numPages"
        :key="i"
        :page="i"
        :src="url2"
        
      ></pdf>

    </div>
    <!-- <div>
      <iframe
        class="iframe"
        :src="url"
      ></iframe>
    </div> -->

    <div class="bottom-btns m-10 btns">
        <eagle-button style="flex:1;" class="mls"  @click="handleClose(1)" v-if="signState ==0||signState== 2">检查人签名</eagle-button>
        <eagle-button style="flex:1;" class="mls" type="orange" @click="handleClose(2)" v-if="signState == 0 || signState == 1">被检查企业签名</eagle-button>
      </div>
  </div>
</template>

<script>
import pdf from 'vue-pdf'
import { mapGetters } from "vuex";
import EagleSingDiy from '../dangerCheckTask/eagle-sing-diy.vue';
export default {
  components: {
    EagleSingDiy, EagleSingDiy,
    pdf,
  },
  data() {
    return {
      adds: "",
      zIndex: -8,
      isCertificate: true,
      isCertificate2: false,
      stageInfo: "",
      client: {},
      points: [],
      canvasTxt: null,
      startX: 0,
      startY: 0,
      moveY: 0,
      moveX: 0,
      endY: 0,
      endX: 0,
      w: null,
      h: null,
      isDown: false,
      isViewAutograph: this.$route.query.isViews > 0,
      contractSuccess: this.$route.query.contractSuccess,

      url: ``,
      url2: ``,
      numPages: 0, // pdf 总页数
      TypeName: null,
      isSignature: null,
      isSignatureBool: false,
      // id: this.$route.query.IDS,
      id: this.$route.query.id,
      DocCode: this.$route.query.DocCode,
      signState: this.$route.query.signState,
      isSign:this.$route.query.isSign,
    };
  },
  created() {
    console.log(this.$route.query.signState)
    this.$toast.loading({
      message: "加载中...",
      forbidClick: true,
    });
  },

  computed: {
    ...mapGetters(["baseUrl", "CompanyCode", "roles"]),
  },

  mounted() {
    this.url2=this.url = this.$route.query.url;
    let  ext=this.url.substring(this.url.lastIndexOf('.'))
    if(this.signState=='1'||this.signState=='2'){
          this.url2=this.url.replace(ext,'-temp'+ext)
    }else if(this.signState=='3'||this.isSign=='true'){

          this.url2=this.url.replace(ext,'-sign'+ext)
    }

    this.TypeName = this.$route.query.TypeName;
    this.isSignature = this.$route.query.isSignature;

    this.DocCode = this.$route.query.DocCode;
    if (
      this.isSignature == "isSignature" ||
      this.isSignature == "forSignature" ||
      this.isSignature == "ProjectEntrustedOnlineDoc" ||
      this.isSignature == "SitePromiseRecordSign" ||
      this.isSignature == "SiteSafeCostPlanMain" ||
      this.isSignature == "ResponsibilityOrgPostUserDepositManage"
    ) {
      this.isSignatureBool = true;

      let canvas = this.$refs.canvasF;
      canvas.height = this.$refs.canvasHW.offsetHeight - 500;
      canvas.width = this.$refs.canvasHW.offsetWidth - 50;
      this.canvasTxt = canvas.getContext("2d");
      this.stageInfo = canvas.getBoundingClientRect();
    }
    this.getNumPages();
    
    if (localStorage.getItem('isRefresh') === "true") {
      console.log(typeof localStorage.getItem('isRefresh'))
      localStorage.setItem('isRefresh', "false")
      location.reload()
    }
  },
  methods: {
    getNumPages() {
      let loadingTask = pdf.createLoadingTask(this.url2);
      loadingTask.promise
        .then((pdf) => {
          this.numPages = pdf.numPages;
        })
        .catch(err => {
          console.error("pdf 加载失败", err);
        });
    },
    download(){
      window.open(this.url2);
    },
    // SiteAppointmentSign(data).then.((res)
    //         =>{

    //         });

        // 签名
        handleClose(add){
      this.adds = add;
      this.zIndex = 8;
      this.isCertificate = true;
      this.isCertificate2 = true;
      this.$refs.setzIndex.setzIndex(this.zIndex);
      // this.overwrite();
    },
       // 签字
   FilePathClick(FilePath) {
      this.imgUrl = FilePath;
      this.handleSign()
    },
    handleSign(){
      let url = this.adds=='1'?"/DangerReportJG/CheckerSign": "/DangerReportJG/Sign"
      this.$axios
          .post(url,{ ID: this.id,SignAttach:this.imgUrl })
          .then((res)=>{
            this.$toast.success("签名成功");
            localStorage.setItem('isRefresh', "true")
            this.$router.push({
              name: "CheckReportDetail",
              query: {
                url:this.url,
                signState:res.data,
                isSign:true,
                id:this.id,
              },
            });
          })
    },
  },
};
</script>

<style lang="less" scoped>

.bottom-btns {
  margin-top: 10px;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .submit-btn {
    width: 32%;
    height: 35px;
    font-size: 14px;
  }
  .delete-btn {
    width: 100%;
    height: 35px;
    font-size: 14px;
  }
}

.read-pdf {
  min-height: 100vh;
}

.button-fixed {
  position: fixed;
  bottom: 0px;
}
.popup2 {
  width: 100%;
  height: 100%;
  padding-top: 16px;
  padding-bottom: 16px;
  text-align: center;
  border-radius: 7px;
}
.user-info {
  min-height: 100vh;
  background-color: #fff;
}
.imgCanvas {
  /* background-color: #71b900; */
  border: 1px solid #7d7d7d;
  width: 50px;
  height: 50px;
}
// .signatureBox-popup {
//   opacity: 1;
//   background: #666;
//   display: none;
//   position: fixed;
//   left: 0;
//   top: 0;
//   width: 100%;
//   height: 100%;
//   display: flex;
//   padding-top: 200px;
// }

.signatureBox {
  width: 100%;
  height: calc(100% - 50px);
  box-sizing: border-box;
  overflow: hidden;
  background: #fff;
  height: 60%;
  z-index: 8;
  display: inline-flex;
  flex-direction: column;
}
.canvasBox {
  box-sizing: border-box;
  flex: 1;
  height: 100%;
}
canvas {
  margin: 20px;
  width: 90%;
  /* height: 200px; */
  border: 1px solid #7d7d7d;
}
.btnBox {
  padding: 10px;
  text-align: center;
}
.btnBox button:first-of-type {
  background: transparent;
  border-radius: 4px;
  height: 40px;
  width: 80px;
  font-size: 14px;
}
.btnBox button:last-of-type {
  background: #71b900;
  color: #fff;
  border-radius: 4px;
  height: 40px;
  width: 80px;
  font-size: 14px;
}
.iframe {
  width: 100%;
  height: calc(100vh);
}
.mls{
    margin-right: 5px;
  }
</style>